#include "connectfour.h"
#include <iostream>

namespace CS270Game {

void ConnectFour::takeTurn(ConnectFour::CFToken player) {
  bool colValid = false;
  int col;
  while (!colValid) {
    std::cout << "Enter a column: ";
    std::cin >> col;

    if (!board.makeMove(col, currentPlayer)) {
      std::cout << "Invalid move." << std::endl;
    } else {
      colValid = true;
    }
  }
}

ConnectFour::CFToken ConnectFour::playgame() {
  std::cout << "Let's play a game!" << std::endl;
  while(board.isWin() == -1 && !board.isFull()) {
    board.printBoard();
    std::cout << "Player " << currentPlayer << std::endl;
    takeTurn();
  }
  //The final game state
  board.printBoard();

  if (board.isFull()) {
    std::cout << "The game is a draw." << std::endl;
    return ConnectFour::EMPTY;
  } else {
    //Must have been a win - isWin function returns an integer equal to 
    // the CFToken of the winning player
    ConnectFour::CFToken winner = ConnectFour::CFToken(board.isWin());
    std::cout << "The game is over, player " << winner << " wins!"
      << std::endl;
    return winner;
  }
}

}
